package org.example.lost.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.domain.PageInfoDTO;
import org.example.domain.Result;

import org.example.lost.entity.UserInfo;
import org.example.lost.entity.dto.UserDTO;
import org.example.lost.entity.vo.RoleInfoVO;
import org.example.lost.entity.vo.UserInfoVO;
import org.example.lost.entity.vo.UserLoginVO;
import org.example.lost.entity.vo.UserUpdateVO;
import org.example.lost.service.UserService;
import org.example.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户子模块的功能
 * 1、登录
 * 2、列表查询用户信息
 * 3、删除指定用户
 * 4、修改用户密码
 *
 * @Author hxy
 * @Date 2021/3/11 22:46
 * @Version 1.0
 */
@RestController
@RequestMapping("/core")
@Api(tags = "用户系统")
//@CrossOrigin(allowCredentials = "true",allowedHeaders="*")
public class UserController {
    @Autowired
    UserService userService;


    @PostMapping("/user/login")
    @ApiOperation("登录")
    public Result<UserInfo> login(@RequestBody UserLoginVO userLoginVO){
        UserInfo userInfo = userService.login(userLoginVO);
        if(userInfo == null){
            return Result.fail("用户名或者密码错误");
        }
        try {
            String token = JwtTokenUtil.createToken(userInfo.getUserId());
            userInfo.setToken(token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(userInfo);
    }


    @GetMapping("/user/phone/{userPhone}")
    @ApiOperation("根据手机号获取用户信息")
    public Result<UserDTO> getOne(@PathVariable(name = "userPhone") @ApiParam(name = "userPhone",value = "用户电话") String userPhone){
        UserDTO userDTO = userService.getOne(userPhone);
        return Result.success(userDTO);
    }

    @PostMapping("/users")
    @ApiOperation("列表查询用户信息")
    public Result<PageInfoDTO<UserInfo>> getList(@RequestBody UserInfoVO userInfoVO){
        PageInfoDTO<UserInfo> userInfos = userService.getList(userInfoVO);
        return Result.success(userInfos);
    }

    @DeleteMapping("/user/{userId}")
    @ApiOperation("删除指定用户")
    public Result<String> delete(@PathVariable(name = "userId") @ApiParam(name = "userId",value = "用户id") String userId){
        int result = userService.delete(userId);
        if(result == 1){
            return Result.success("删除用户成功");
        }
        return Result.fail("删除用户失败");
    }

    @PutMapping("/user/password")
    @ApiOperation("修改用户密码")
    public Result<String> updatePassword(@RequestBody UserUpdateVO userUpdateVO){
        int result = userService.updatePassword(userUpdateVO);
        if(result == 1){
            return Result.success("修改成功");
        }
        else if(result == 2){
            return Result.fail("旧密码错误");
        }
        return Result.success("修改失败");
    }

    @GetMapping("/user/{userId}")
    @ApiOperation("根据id获取指定用户信息")
    public Result<UserInfo> getUser(@PathVariable(name = "userId") @ApiParam(name = "userId",value = "用户id") String userId){
        UserInfo userInfo = userService.getUser(userId);
        return Result.success(userInfo);

    }

    @PutMapping("/user")
    @ApiOperation("更新用户信息")
    public Result<String> updateUser(@RequestBody UserInfo userInfo){
        int result = userService.updateUser(userInfo);
        if(result == 1){
            return Result.success("修改成功");
        }
        return Result.success("修改失败");
    }


    @PostMapping("/user")
    @ApiOperation("添加用户信息")
    public Result<String> addUser(@RequestBody UserInfo userInfo){
        int result = userService.addUser(userInfo);
        if(result == 1){
            return Result.success("添加成功");
        }
        return Result.success("添加失败");
    }

    @PostMapping("/user/role")
    @ApiOperation("修改角色信息")
    public Result<String> updateRole(@RequestBody RoleInfoVO roleInfoVO){
        int result = userService.updateRole(roleInfoVO);
        if(result == 1){
            return Result.success("修改成功");
        }
        return Result.success("修改失败");
    }



}
